#测试多线程的操作
#一个线程是写入log 1 每秒两次
#一个线程是写入log 2 每秒一次

import threading
from time import sleep,ctime

loops = [2,4]

def writelog(msg):
    file = open("./logFile.log","a+")
    file.write(msg)
    file.close()
    
def loop(nloop,nsec):
    while 1:
        msg = ("loop :%d nesc :%d \n"%(nloop,nsec))
        print(msg)
        writelog(msg)
        sleep(nsec)
        print ("loop end Time :",ctime())

def main():
    print ("main start at :",ctime())
    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = threading.Thread(target = loop,args=(i,loops[i])) #线程函数 参数
        threads.append(t)
    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join() #等待结束

if __name__ == "__main__":
    main()
